<?php


if (!function_exists('walletLog')) {
    /**
     *  金额变动记录
     * @param $user_id 用户id
     * @param $order_num 订单号
     * @param $log_type 交易类型:order=订单,withdraw=提现,recharge=充值,invite=邀请
     * @param $sign 增减:inc=增加,dec=减少
     * @param $money 变更金额
     * @param $remarks 备注
     */
    function walletLog($user_id,$order_num,$log_type,$sign,$money,$remarks)
    {
        $model = new \app\api\model\UserWalletLogModel();
        $model_user = new \app\admin\model\User();
        $model_wallet = new \app\api\model\UserWalletModel();

        //用户类型
        $type = $model_user->where('id',$user_id)->value('type');
        //获取用户钱包
        $now_money = $model_wallet->where('user_id', $user_id)->value('money');
        $before_money = $sign == 'inc' ? $now_money - $money : $now_money + $money;
        $after_money = $sign == 'dec' ? $now_money + $money : $now_money - $money;
        //生成记录
        $model->insert([
            'user_id' => $user_id,
            'type' => $type, //用户身份
            'order_num' => $order_num,
            'log_type' => $log_type, //记录类型
            'before_money' => $before_money, //变更前
            'after_money' => $after_money, //变更后
            'sign' => $sign, //增减:inc=增加,dec=减少
            'money' => $money, //变更金额
            'remarks' => $remarks,
            'createtime' => time(),
        ]);
    }
}
